import java.io.*;
import java.util.*;

// 会员类
class Member implements Serializable {
    private static final long serialVersionUID = 1L;
    String cardNumber; // 卡号
    String name; // 姓名
    int points; // 积分

    public Member(String cardNumber, String name, int points) {
        this.cardNumber = cardNumber;
        this.name = name;
        this.points = points;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public String getName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    public void addPoints(int points) {
        this.points += points;
    }

    public void usePoints(int points) {
        if (points <= this.points) {
            this.points -= points;
        } else {
            System.out.println("积分不足，无法兑换");
        }
    }
}

// 超市类，包含会员信息
class Supermarket implements Serializable {
    private static final long serialVersionUID = 1L;
    String name; // 超市名称
    String address; // 超市地址
    Map<String, Member> members; // 会员信息

    public Supermarket(String name, String address) {
        this.name = name;
        this.address = address;
        this.members = new HashMap<>();
    }

    public void addMember(String cardNumber, String name, int points) {
        if (!members.containsKey(cardNumber)) {
            members.put(cardNumber, new Member(cardNumber, name, points));
            System.out.println("开卡成功");
        } else {
            System.out.println("卡号已存在");
        }
    }

    public Member getMember(String cardNumber) {
        return members.get(cardNumber);
    }
}


public class test {
    private static final String SERIAL_FILE = "supermarket.ser";

    public static void main(String[] args) {
        Supermarket supermarket;
        try {
            // 尝试读取序列化文件
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(SERIAL_FILE));
            supermarket = (Supermarket) ois.readObject();
            ois.close();
        } catch (Exception e) {
            // 文件不存在或读取错误，创建新的超市对象
            supermarket = new Supermarket("Moonshot Supermarket", "123 Moonshot Road");
        }

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：1. 开卡 2. 查询积分 3. 退出");
            String option = scanner.nextLine();
            switch (option) {
                case "1":
                    System.out.println("请输入卡号：");
                    String cardNumber = scanner.nextLine();
                    System.out.println("请输入姓名：");
                    String name = scanner.nextLine();
                    System.out.println("请输入积分：");
                    int points = scanner.nextInt();
                    supermarket.addMember(cardNumber, name, points);
                    break;
                case "2":
                    System.out.println("请输入卡号：");
                    cardNumber = scanner.nextLine();
                    Member member = supermarket.getMember(cardNumber);
                    if (member != null) {
                        System.out.println("会员：" + member.getName() + "，积分：" + member.getPoints());
                    } else {
                        System.out.println("未找到该会员");
                    }
                    break;
                case "3":
                    try {
                        // 保存超市对象到序列化文件
                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(SERIAL_FILE));
                        oos.writeObject(supermarket);
                        oos.close();
                        System.out.println("退出系统，数据已保存");
                        System.exit(0);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    break;
                default:
                    System.out.println("无效操作");
            }
        }
    }
}